package com.yj.wisemoms.temp;


import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import com.yj.wisemoms.R;
import com.yj.wisemoms.Util.Util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class Custom_list_adapter extends ArrayAdapter<ListData> 
{
	private Context mContext;
	private int mLayoutResource;
	private ArrayList<ListData> mList;
	private LayoutInflater mInflater;
	
	private int type; 

	
	public Custom_list_adapter(Context context, int rowLayoutResource, ArrayList<ListData> objects, int type) 
	{
		super(context, rowLayoutResource, objects);
		this.mContext = context;
		this.mLayoutResource = rowLayoutResource;
		this.mList = objects;
		this.mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.type = type;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) 
	{
        
		if (convertView == null) 
		{
			convertView = mInflater.inflate(mLayoutResource, null);
		}

		ListData mlistdata = getItem(position);


		if (mlistdata != null) 
		{
			switch(type)
			{
			// teacher_list
			case 1:
				
				//ImageView teacher_image = (ImageView) convertView.findViewById(R.id.teacher_picture);
				TextView teacher_name = (TextView) convertView.findViewById(R.id.teacher_name);
				TextView teacher_sex = (TextView) convertView.findViewById(R.id.teacher_sex);
				TextView teacher_school = (TextView) convertView.findViewById(R.id.teacher_school);
				TextView teacher_subject = (TextView) convertView.findViewById(R.id.teacher_subject);
				TextView teacher_experience = (TextView) convertView.findViewById(R.id.teacher_experience);
				ImageView ribbon = (ImageView) convertView.findViewById(R.id.ribbon);
				
			
				//
				ViewHolder viewHolder = null;
				viewHolder = new ViewHolder();
				viewHolder.imageView = (ImageView)convertView.findViewById(R.id.teacher_picture);
				convertView.setTag(viewHolder);
				viewHolder = (ViewHolder)convertView.getTag();
			
				viewHolder.imageURL = mlistdata.imageurl;
				new DownloadAsyncTask().execute(viewHolder);
				
				//
				
				teacher_name.setText(mlistdata.getStr1());
				teacher_sex.setText(mlistdata.getStr2());
				teacher_school.setText(mlistdata.getStr3());
				teacher_subject.setText(mlistdata.getStr4());
				teacher_experience.setText(mlistdata.getStr5());
				ribbon.setVisibility(View.INVISIBLE);
				if(mlistdata.getRecommand() >= 3){
					
					if(mlistdata.getRecommand() == 3){
						ribbon.setImageResource(R.drawable.ribbon_special);
					}else if(mlistdata.getRecommand() == 4){
						ribbon.setImageResource(R.drawable.ribbon_premium);
					}
					
					ribbon.setVisibility(View.VISIBLE);
				}
				
				
				break;
			
			case 2:
				TextView title = (TextView) convertView.findViewById(R.id.teacher_name);
				TextView date = (TextView) convertView.findViewById(R.id.teacher_sex);
				
				title.setText(mlistdata.getStr1());
				date.setText(mlistdata.getStr1());
				break;
			
			}
		}
		return convertView;
	}
	@Override
	public int getCount(){return mList.size();}

	@Override
	public ListData getItem(int position){return mList.get(position);}

	@Override
	public int getPosition(ListData item){return mList.indexOf(item);}
	
	private static class ViewHolder {
		ImageView imageView;
		String imageURL;
		Bitmap bitmap;
	}
		
	private class DownloadAsyncTask extends AsyncTask<ViewHolder, Void, ViewHolder> {

		@Override
		protected ViewHolder doInBackground(ViewHolder... params) {
			// TODO Auto-generated method stub
			//load image directly
			ViewHolder viewHolder = params[0];
			try {
				URL imageURL = new URL(viewHolder.imageURL);
				viewHolder.bitmap = BitmapFactory.decodeStream(imageURL.openStream());
			} catch (IOException e) {
				// TODO: handle exception

			}
			
			return viewHolder;
		}
		
		@Override
		protected void onPostExecute(ViewHolder result) {
			// TODO Auto-generated method stub
			if (result.bitmap == null) {
				result.imageView.setImageResource(R.drawable.no_image);
			} else {
				result.imageView.setImageBitmap(result.bitmap);
			}
		}
	}
}
